package com.sinodata.bsm.cicp.collector.IIS6;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.constants.SeparatorConstants;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class AppPool extends Collector {

    private static Logger logger = Logger.getLogger(AppPool.class);
    static final String cmdStr = "iispoolinfo.exe";
    static final String regStr = "\\[(.+)\\]\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)";

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = null;
        ResPropertyValue.PropValue[] propValues = null;

        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, cmdStr, 0, 0);
            //reader = CollectorUtil.getReader(cmdStr, ip, TIMEOUT_MIDDLE_INTERVAL);
            // reader读入的每行数据
            String line = null;
            Pattern p = Pattern.compile(regStr);
            Matcher match = null;
            StringBuffer sb = new StringBuffer();
            sb.append("Name").append(SeparatorConstants.COL_SEPARATOR);
            sb.append("PID").append(SeparatorConstants.COL_SEPARATOR);
            sb.append("CPU_Time").append(SeparatorConstants.COL_SEPARATOR);
            sb.append("CPU_Total_Time").append(SeparatorConstants.COL_SEPARATOR);
            sb.append("Memory_Usage").append(SeparatorConstants.COL_SEPARATOR);
            sb.append("Max_Memory").append(SeparatorConstants.COL_SEPARATOR);
            sb.append("Thread_Count").append(SeparatorConstants.COL_SEPARATOR);
            sb.append("IO_Read").append(SeparatorConstants.COL_SEPARATOR);
            sb.append("IO_Write").append(SeparatorConstants.COL_SEPARATOR);
            sb.append(SeparatorConstants.LIN_SEPARATOR);
            //名称 PID CPU时间 CPU总时间 内存使用 最大内存 线程数 IO读 IO写
            while ((line = reader.readLine()) != null) {
                match = p.matcher(line);
                if (match.matches()) {
                    propertyValueX = new ResPropertyValue();
                    propValues = new ResPropertyValue.PropValue[propMetas.length];
                    for (int i = 0; i < propMetas.length - 2; i++) {
                        propValues[i] = ResPropertyValue.createPropValue(match.group(i + 1), propMetas[i]);
                        sb.append(propValues[i].value + SeparatorConstants.COL_SEPARATOR);
                    }
                    propValues[propMetas.length - 1] = ResPropertyValue.createPropValue(match.group(1), propMetas[propMetas.length - 1]);
                    sb.append(propValues[propMetas.length - 1].value + SeparatorConstants.COL_SEPARATOR);
                    sb.append(SeparatorConstants.LIN_SEPARATOR);
                } else {
                    logger.warn("collect iis app pool information line not match regex string!\n" + line + "\n" + regStr);
                }

            }
            //propValues[9] = ResPropertyValue.createPropValue(sb.toString(), propMetas[9]);
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
            return values;
        } catch (Throwable t) {
            logger.error("collect iis app pool information error", t);
            throw new CollectorException(t);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (java.io.IOException e) {
                    logger.error("collect iis app pool information error", e);
                    throw new CollectorException(e);
                }
            }
        }
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "" };
    }

    @Override
    public String[] getPropMeta() {
        //return new String[] { "名称", "PID", "CPU时间", "CPU总时间", "内存使用", "最大内存", "线程数", "IO读", "IO写", "AppPool" };
        //        return new String[] { "Name", "PID", "CPU_Time", "CPU_Total_Time", "Memory_Usage", "Max_Memory", "Thread_Count", "IO_Read", "IO_Write", "AppPool" };
        return new String[] { "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9", "$10" };
    }

}
